// 
package cachex

import "strings"

type IModify interface {
	// 修正是否生效
	Valid(ctx interface{}, field string) bool
	// 修正后的值
	Value(ctx interface{}, field string, origin interface{}) (last interface{})
	// 修正后的数组长度
	Length() int
}

// paht = /field1/field2 or field1/field2
func GetObjectByPath(object IObject, path string) IObject {
	if string(path[0]) == "/" {
		path = path[1:]
	}
	pathArr := strings.Split(path, "/")
	cur := object
	for {
		if len(pathArr) == 0 {
			break
		}
		field := pathArr[0]
		pathArr = pathArr[1:]
		cur = object.RefGetObject(field)
	}
	return cur
}

// path = /field1/field2 or field1/field2
func GetObjectFieldByPath(object IObject, path string) (obj IObject, field string) {
	if string(path[0]) == "/" {
		path = path[1:]
	}
	pathArr := strings.Split(path, "/")
	cur := object
	for {
		if len(pathArr) == 0 {
			break
		}
		f := pathArr[0]
		pathArr = pathArr[1:]
		if !cur.RefIsObject(f) {
			return cur, f
		}
		cur = cur.RefGetObject(f)
	}
	return cur, ""
}
